package com.carambatv.android.utils;

import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.StringTokenizer;

import org.apache.http.HttpException;
import org.xmlpull.v1.XmlPullParser;
import org.xmlpull.v1.XmlPullParserFactory;

import android.content.ContentProviderOperation;
import android.content.ContentResolver;
import android.util.Log;

import com.carambatv.android.db.VideoColumns;
import com.carambatv.android.db.CarambaDBSchema;
import com.carambatv.android.model.VideoObject;

public class YtubeProcessor {
	public static final String ENTRY = "entry";
	public static final String ID = "id";
	public static final String PUBLISHED = "published";
	public static final String TITLE = "title";
	public static final String THUMBNAIL = "thumbnail";
	public static final String URL = "url";
	public static final String HEIGHT = "height";
	public static final String i360 = "360";
	public static final String TOTAL_RESULTS = "totalResults";
	public static int total = 0;

	private String thumbnail;
	private String id = null;
	private String title = null;
	private String date = null;

	public void processInternal(final ContentResolver resolver, final InputStream in, String type) throws IOException, HttpException, Exception {
		ArrayList<ContentProviderOperation> insertOperations = new ArrayList<ContentProviderOperation>();
		try {

			XmlPullParserFactory factory = XmlPullParserFactory.newInstance();
			factory.setNamespaceAware(true);
			XmlPullParser xpp = factory.newPullParser();
			xpp.setInput(in, "UTF-8");

			int eventType = xpp.getEventType();
			while (eventType != XmlPullParser.END_DOCUMENT) {
				if (eventType == XmlPullParser.START_TAG && xpp.getName().equals(ENTRY)) {
					id = null;
					thumbnail = null;
					title = null;
					date = null;
				} else if (eventType == XmlPullParser.START_TAG && xpp.getName().equals(TOTAL_RESULTS)) {
					eventType = xpp.next();
					total = Integer.valueOf(xpp.getText());
					Log.e(TOTAL_RESULTS, xpp.getText());
				} else if (eventType == XmlPullParser.START_TAG && xpp.getName().equals(ID)) {
					eventType = xpp.next();
					StringTokenizer st1 = new StringTokenizer(xpp.getText(), "/");
					do {
						id = st1.nextToken();
					} while (st1.hasMoreElements());
				} else if (eventType == XmlPullParser.START_TAG && xpp.getName().equals(PUBLISHED)) {
					eventType = xpp.next();
					date = xpp.getText();
				} else if (eventType == XmlPullParser.START_TAG && xpp.getName().equals(TITLE)) {
					eventType = xpp.next();
					title = xpp.getText();
				} else if (eventType == XmlPullParser.START_TAG && xpp.getName().equals(THUMBNAIL)) {
					int size = xpp.getDepth();
					for (int i = 0; i < size; i++) {
						String attrName = xpp.getAttributeName(i);
						String attrValue = xpp.getAttributeValue(i);

						if (attrName != null && attrName.equals(URL) && thumbnail == null) {
							thumbnail = attrValue;
						}

						if (attrName != null && attrName.equals(HEIGHT) && attrValue != null && attrValue.equals(i360)) {
							break;
						}
					}
				} else if (eventType == XmlPullParser.END_TAG && xpp.getName().equals(ENTRY)) {
					insertOperations.add(ContentProviderOperation.newInsert(VideoObject.CONTENT_URI).withValue(VideoColumns.ID_VIDEO.getName(), id)
							.withValue(VideoColumns.TITLE.getName(), title).withValue(VideoColumns.THUMBNAIL.getName(), thumbnail)
							.withValue(VideoColumns.TYPE.getName(), type).withValue(VideoColumns.DATE.getName(), DateUtil.parseDateToMillisecinds(date))
							.build());
				}
				eventType = xpp.next();
			}
		} finally {
			IOUtil.closeQuietly(in);
		}
		resolver.applyBatch(CarambaDBSchema.CONTENT_AUTHORITY, insertOperations);
	}
}
